var page = require('webpage').create();
var tabUrl = 'https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&srcqid=719648380286854694&tn=99682755_hao_pg&wd=pcsoft&oq=%25E7%2599%25BE%25E5%25BA%25A6&rsv_pq=c2479c5e00110899&rsv_t=c3d6NnDVAJPDIOsLtdAFpvFdoI6XG34t4YJcnsc%2FrQktgsxCmIP1G2%2BzOLggbNPoTem7hCzR&rqlang=cn&rsv_enter=1&rsv_sug3=10&rsv_sug1=5&rsv_sug7=100&bs=%E7%99%BE%E5%BA%A6'
page.open(tabUrl,function(status) {
	if(status !== "success"){
		console.log('fail');
		phantom.exit();
	}
	var result = page.evaluate(main_dealer);
	page.render('liu.png');
	phantom.exit();
})
var main_dealer = function(){
//	document.getElementsByTagName('a')[0].click();
//	document.getElementsByTagName('button')[0].click();
//	document.getElementsByTagName('div')[0].click();
//	var clickEvent = document.createEvent("MouseEvents");
//	clickEvent.initMouseEvent("click",true,true, window, 1, 0, 0);
	var wrap = document.getElementById("content_left");
	var aWrap = wrap.getElementsByClassName('f13')[1];
	var a = aWrap.getElementsByClassName('c-showurl')[0];
//	a.dispatchEvent(clickEvent);
	console.log(aWrap.innerHTML)
	console.log(a.innerHTML);
	var d = a.href;
	console.log("建立额"+d);
	a.onclick();
	
	console.log(window.location.href);
	
}
page.onConsoleMessage = function(msg) {
  console.log('The inner msg: ' + msg + '|');
};
page.onNavigationRequested = function() {
	console.log("页面发生跳转");
}
//
//
//function mouseclick( element ) {
//  // create a mouse click event
//  var event = document.createEvent( 'MouseEvents' );
//  event.initMouseEvent( 'click', true, true, window, 1, 0, 0 );
// 
//  // send click to element
//  element.dispatchEvent( event );
//}
// 
//function handle_page( page ) {
//  page.evaluate(
//      function( mouseclick_fn ) {
//          var element = document.querySelector( "input#payConf" );
//          mouseclick_fn( element );
//      },
//      mouseclick
//  );
// 
//  window.setTimeout(
//      function () {
//          handle_click_reaction( page );
//      },
//      5000 // give page 5 seconds to process click
//  );
//}



//function clickElement(id){
//var a = document.getElementById(id);
//
//var e = document.createEvent('MouseEvents');
//e.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
//
//a.dispatchEvent(e);
//}
//
//if(phantom.state.length !== 0){
//// save screenshot for every page / state
//phantom.viewportSize = {width: 800, height: 600};
//phantom.sleep(2000);
//phantom.render('screen_' + phantom.state + '.png');
//}
//
//if(phantom.state.length === 0){
//phantom.state = '0_home';
//phantom.open('http://www.mini.de');
//}
//else if(phantom.state === '0_home'){
//phantom.state = '1_config';
//
//clickElement('quicklink_id1');
//}
//else if(phantom.state === '1_config'){
//phantom.exit();
//}